package com.crm.miaohe.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.crm.miaohe.controller.PageResponse;
import com.crm.miaohe.controller.openid.request.UserOpenidQueryParams;
import com.crm.miaohe.mapper.TblUserOpenidMapper;
import com.crm.miaohe.model.TblUserOpenid;
import com.crm.miaohe.model.TblUserOpenidExample;
import com.crm.miaohe.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserOpenidServiceImpl implements UserOpenidService {
	
	private TblUserOpenidMapper tblUserOpenidMapper;

	
	public UserOpenidServiceImpl(TblUserOpenidMapper tblUserOpenidMapper) {
		this.tblUserOpenidMapper = tblUserOpenidMapper;
	}


	@Override
	public PageResponse<TblUserOpenid> getOpenidDetails(UserOpenidQueryParams params) {
		TblUserOpenidExample example = new TblUserOpenidExample();
		TblUserOpenidExample.Criteria criteria = example.createCriteria();
	    
		
		if (!ObjectUtils.isEmpty(params.getUserName())) {
            criteria.andUserNameLike(StringUtils.quoteWith(params.getUserName(), "%"));
        }
		
        PageInfo<TblUserOpenid> pageInfo =
                PageHelper.startPage(params.getPageNo(), params.getPageSize(), "create_time desc")
                        .doSelectPageInfo(() -> tblUserOpenidMapper.selectByExample(example));
        
        return PageResponse.<TblUserOpenid>builder()
                .currentPage(pageInfo.getPageNum())
                .totalPage(pageInfo.getPages())
                .totalElement(pageInfo.getTotal())
                .records(pageInfo.getList())
                .build();
	}


	@Override
	public void deleteUserOpenid(String id) {
		TblUserOpenidExample example = new TblUserOpenidExample();
		TblUserOpenidExample.Criteria criteria = example.createCriteria();
		criteria.andOpenIdEqualTo(id);
		tblUserOpenidMapper.deleteByExample(example);
		
	}


	@Override
	public List<TblUserOpenid> findUserOpenid(String id) {
		TblUserOpenidExample example = new TblUserOpenidExample();
		TblUserOpenidExample.Criteria criteria = example.createCriteria();
		criteria.andOpenIdEqualTo(id);
		List<TblUserOpenid> list = tblUserOpenidMapper.selectByExample(example);
		return list;
	}

}
